
package edu.upc.clase.demo.dao.impl;

import edu.upc.clase.demo.dao.PersonaDao;
import edu.upc.clase.demo.entity.Persona;
import java.util.List;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author dramos
 */
@Repository
public class PersonaDaoImpl extends SimpleJdbcDaoSupport implements PersonaDao {
    
    private static Logger log = LoggerFactory.getLogger(PersonaDaoImpl.class);

    @Autowired
    public PersonaDaoImpl(DataSource dataSource) {
            log.info("Asignando el dataSource");
            setDataSource(dataSource);
    }
    
    @Override
    public Integer insertar(Persona persona) {
        
        getJdbcTemplate().update(
                "insert into personas (idpers,tippers,nompers,appat,apmat,sexo,tipdoc,numdoc,email,passw,fecnac,nrocel) "
                                      + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                persona.getIdpers(),persona.getTippers(),persona.getNompers(),
                persona.getAppat(),persona.getApmat(),persona.getSexo(),
                persona.getTipdoc(),persona.getNumdoc(),persona.getEmail(),
                persona.getPassw(),persona.getFecnac(),persona.getNrocel());
        return getSimpleJdbcTemplate().queryForInt("call identity()");
    }

    @Override
    public List<Persona> buscarTodos() {
        return getSimpleJdbcTemplate().query(
                        "select * from personas",
                        new BeanPropertyRowMapper<Persona>(Persona.class));
    }
        
    
    
    
    
    
}
